//{
//"type" : "消息类型",
//"id" : "消息ID",
//"content" : "消息内容",
//"to" : "接收者",
//"from" : "发送人",
//"token" : "令牌"
//}
//{
//"type" : "消息类型",
//"id" : "消息ID"
//}

import 'dart:convert';

const MESSAGE_TABLE_SQL = """
  CREATE TABLE t_messages 
  (
    id CHAR(36) PRIMARY KEY, 
    type CHAR(10),
    content TEXT,
    toUser CHAR(20),
    fromUser CHAR(20),
    time CHAR(20),
    receipt INTEGER
  )
  """;

class MessageType {
  static const text = "TEXT";
  static const receiptServer = "RECEIPT_SERVER";
  static const receiptClient = "RECEIPT_CLIENT";

  static const add = "ADD";
  static const ping = "PING";
}
class MessageModel {
  final String type;
  final String id;
  final String content;
  final String toUser;
  final String fromUser;
  final String token;
  final String time;
  final int receipt;

  MessageModel(this.type, this.id, this.content, this.toUser, this.fromUser,
      this.token, this.time, this.receipt);

  String toJson() {
    return """
    {
      "type" : "${type}",
      "id" : "${id}",
      "content" : "${content}",
      "to" : "${toUser}",
      "from" : "${fromUser}",
      "token" : "${token}",
      "time" : "${time}"
    }
    """;
  }

  static MessageModel fromJson(dynamic map) {
    return MessageModel(map["type"], map["id"], map["content"],
        map["to"], map["from"], map["token"], map["time"], map["receipt"]);

  }


  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
          other is MessageModel &&
              runtimeType == other.runtimeType &&
              id == other.id;

  @override
  int get hashCode => id.hashCode;

  @override
  String toString() {
    return 'MessageModel{type: $type, id: $id, content: $content, toUser: $toUser, fromUser: $fromUser, token: $token, time: $time, receipt: $receipt}';
  }


}

void main() {
  String messageModel = """
  {
    "type" : "success",
    "id" : "1001"
    
  }
  """;
  final model = MessageModel.fromJson(json.decode(messageModel));
  print("model: $model");
  print(model.toJson());
}